package com.tshopping.service.impl;

import com.tshopping.bean.*;
import com.tshopping.dao.CartDao;
import com.tshopping.dao.GoodsDao;
import com.tshopping.dao.ParameterDao;
import com.tshopping.service.CartService;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.rowset.CachedRowSet;
import java.util.ArrayList;

/**
 * @name: CartServiceImpl
 * @description: 购物车业务层处理类
 * @author: Jackeys
 * @date: 2021-01-27 20:37
 */
@Service(value = "cartService")
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private ParameterDao parameterDao;

    @Override
    /**
     * @description 返回用户购物车
     * @Param: user
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-28 09:38
     */
    public ResponseData getCartsByUser(int user, int currentPage) {
        ResponseData responseData=ResponseData.ok();
        Page page=new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);
        //2、每页显示的数据
        int pageSize=5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = cartDao.selectCartCountByUser(user);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start=(currentPage-1)*pageSize;
        int size=pageSize;
        ArrayList<Cart> carts=cartDao.selectCartByUserID(user,start,size);
        ArrayList<Goods> goodsList=new ArrayList<>();
        ArrayList<Parameter> parameters=new ArrayList<>();
        for(Cart cart:carts){
            goodsList.add(goodsDao.selectGoodsByGoodsId(cart.getGoods()));
            parameters.add(parameterDao.selectParameterById(cart.getParameter()));
        }
        responseData.putDataValue("cartList",carts);
        responseData.putDataValue("goodsList",goodsList);
        responseData.putDataValue("parameterList",parameters);
        responseData.putDataValue("page",page);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户加入购物车
     * @Param: user
     * @Param: goods
     * @Param: parameter
     * @Param: number
     * @Param: time
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-28 09:38
     */
    public ResponseData addCart(int user, int goods, int parameter, int number, String time) {
        ResponseData responseData=ResponseData.ok();
        Cart cart=cartDao.selectCartByGoodsIdAndParameter(goods,parameter,user);
        if(cart==null){
            responseData.putDataValue("flag",cartDao.insertCart(user,goods,parameter,number,time));
            return responseData;
        }else{
            responseData.putDataValue("flag",cartDao.updateCartById(number+cart.getNumber(),cart.getId()));
            return responseData;
        }
    }

    @Override
    /**
     * @description 处理用户删除购物车
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-28 09:39
     */
    public ResponseData deleteCartById(int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",cartDao.deleteCartById(id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户更新购物车
     * @Param: number
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-28 09:39
     */
    public ResponseData updateCartByNumber(int number, int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",cartDao.updateCartById(number,id));
        return responseData;
    }
}
